//
//  ITFileMD5ViewController.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/4.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "ITFileMD5ViewController.h"
#import <CommonCrypto/CommonDigest.h>
#import "Masonry.h"

@interface ITFileMD5ViewController ()

@property(nonatomic, strong) UIView *moveView;

@property (nonatomic, strong) NSString *tempStr;
@property (nonatomic, copy) void(^bock1)(void);
@end

@implementation ITFileMD5ViewController
-(void)dealloc {
    NSLog(@" ITFileMD5ViewController ITFileMD5ViewController dealloc");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"b1a01a0b6e2b45808e0d_p_1.zip" ofType:nil];
    NSString *md5 = [self getFileMd5WithFilePath:path];
    NSLog(@"文件md5: %@",md5);
    
    NSString *md5Str = [self getMd5:@"--++--++--++--++--++"];
    NSLog(@"字符串md5: %@",md5Str);
    [self setupview];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:14];
    label.text = @"adfdfdff";
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(100);
        make.left.mas_equalTo(0);
    }];
    
    self.tempStr = @"123123123123";
    __weak ITFileMD5ViewController * weakself = self;
    self.bock1 = ^{
        __strong ITFileMD5ViewController *strongSelf = weakself;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            strongSelf.tempStr = @"testPrint3";
            NSLog(@"self.name = %@", strongSelf.tempStr);
            [NSThread sleepForTimeInterval:10];
            NSLog(@"self.name = %@", strongSelf.tempStr);
        });
        
//        NSLog(@"tempStr %@",weakself.tempStr);
//        [[[NSThread alloc] initWithBlock:^{
//            sleep(4);
//            NSLog(@"tempStr %@",weakself.tempStr);
//        }]start] ;
        
    };
    weakself.tempStr = @"---=-=--=-=-=";
    self.bock1();
    NSLog(@"self.name = %@", weakself.tempStr);
}

- (NSString *)getFileMd5WithFilePath:(NSString *)filePath
{
    //生成文件的MD5   校验的是压缩包的MD5  判断下载是否正确
    NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
    if( handle == nil ) {
        NSLog(@"文件出错");
    }
    
    CC_MD5_CTX md5;
    CC_MD5_Init(&md5);
    BOOL done = NO;
    while(!done)
    {
        NSData* fileData = [handle readDataOfLength: 256 ];
        CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
        if( [fileData length] == 0 ) done = YES;
    }
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5_Final(digest, &md5);
    NSString *fileMD5 = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                    digest[0], digest[1],
                    digest[2], digest[3],
                    digest[4], digest[5],
                    digest[6], digest[7],
                    digest[8], digest[9],
                    digest[10], digest[11],
                    digest[12], digest[13],
                    digest[14], digest[15]];
    return fileMD5;
}


- (NSString *)getMd5:(NSString *)string{
    if (!string) return nil;
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(data.bytes, (CC_LONG)data.length, result);
    return [NSString stringWithFormat:
                @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                result[0],  result[1],  result[2],  result[3],
                result[4],  result[5],  result[6],  result[7],
                result[8],  result[9],  result[10], result[11],
                result[12], result[13], result[14], result[15]
            ];
}

-(void)tapAction:(UITapGestureRecognizer *)tap {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"value" object:@1 userInfo:@{@"key":@"value"}];
    NSLog(@"tapAction");
}
-(void)btnAction {
    NSLog(@"btnAction");
}
-(void)setupview {
    
//    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView.userInteractionEnabled = YES;
//    imageView.frame = CGRectMake(100, 100, 100, 100);
//    imageView.backgroundColor = [UIColor blueColor];
//    [self.view addSubview:imageView];
//
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//    btn.backgroundColor = [UIColor redColor];
//    btn.frame = CGRectMake(20, 0, 100, 40);
//    [btn setTitle:@"button" forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
//    [imageView addSubview:btn];
//
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//    [self.view addGestureRecognizer:tap];
    
    self.moveView = [[UIView alloc] initWithFrame:CGRectMake(40, 100, 100, 100)];
    self.moveView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.moveView];
    
}



@end
